package it.basketstats.activities;

import it.basketstats.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.EditText;

public class AddTeamDialog extends DialogFragment {

	public static interface addTeamListener {
		public abstract void addTeam(String name);
	}
	private addTeamListener listener;
	
	// make sure the Activity implemented the interface
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	    try {
	        this.listener = (addTeamListener)activity;
	    }
	    catch (final ClassCastException e) {
	        throw new ClassCastException(activity.toString() + " must implement OnCompleteListener");
	    }
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setMessage(R.string.team_addTeamDialog_title);
		final EditText input = new EditText(getActivity());
		builder.setView(input);
		builder.setCancelable(true);
		builder.setPositiveButton(android.R.string.ok,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// callback function
						listener.addTeam(input.getText().toString());
					}
				});
		builder.setNegativeButton(android.R.string.cancel,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// do nothing
					}
				});

		return builder.create();
	}
}
